package leetcode;

public class Q0162 {
    /**
     * O(n) O(1)
     **/
//    public int findPeakElement(int[] nums) {
//        for (int i = 1; i < nums.length - 1; i++) {
//            if (nums[i] > nums[i - 1] && nums[i] > nums[i + 1]) return i;
//        }
//        return nums[0] > nums[nums.length - 1] ? 0 : nums.length - 1;
//    }


    /**
     * O(logn) O(1)
     **/
    public int findPeakElement(int[] nums) {
        int l = 0, r = nums.length - 1;
        while (l < r - 1) {
            int mid = l + r >> 1;
            if (nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]) return mid;
            else if (nums[mid] > nums[mid + 1]) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        return nums[l] > nums[r] ? l : r;
    }
}
